package com.forenow.practise;

import java.util.Scanner;

public class EmployeeAIpml implements EmployeeDAO {
    //声明一个数据容器
    private Employee[] employees;
    int count;

    public EmployeeAIpml() {
        //利用构造器初始化数组
        employees = new Employee[3];
    }

    @Override
    public void add(Employee e) {
        //1、判断参数e是否为空，如果不为空
        if (e != null) {
            //2、如果count<3
            if (count < 3) {
                    //3、如果e添加到数组容器中，count++
                    employees[count++] = e;
                    //4、输出信息
                    System.out.println("添加成功！");
            }else{
                System.out.println("存不下了");
            }
        }else{
            System.out.println("没有数据");
        }
    }

    @Override
    public Employee[] selectAll() {
        //1.要对原数组进行遍历，每遍历一次就要将原数组中获取的员工对象保存到新数组中
        Employee[] em = new Employee[employees.length];
        for(int i= 0;i<count;i++){
           em[i]=employees[i];
        }
        //2、返回新的数组
        return em;
    }

    @Override
    public void update(Employee employee) {
        for (int i=0;i<count;i++){
            if (employees[i].equals(employee)){
                Scanner sc = new Scanner(System.in);
                System.out.println("请输入要修改的学号");
                int id=sc.nextInt();
                employee.setId(id);
                System.out.println("请输入要修改的名字");
                String name=sc.next();
                employee.setName(name);
                System.out.println("请输入要修改的工资");
                double sal = sc.nextDouble();
                employee.setSal(sal);
                System.out.println("请输入要修改的部门");
                String dept =sc.next();
                employee.setDept(dept);

            }else{
                System.out.println("没有该部门，无法修改");
            }
        }

    }

    @Override
    public Employee selectById(int id) {
        //1、对e的数组进行遍历，
        Employee ee=new Employee();
        for (int i=0;i<count;i++){
            if (employees[i].getId()==id){
                ee = employees[i];
            }
        }
        //2.每遍历一次就要获取到Employee对象的id是否与参数中的id是否相等
        //3.最后将查询到的对象返回
        return ee;
    }

    public static void main(String[] args) {
        Employee e3 = new Employee(1, "jack", 400.3, "人事部");
        EmployeeAIpml employeeAIpml = new EmployeeAIpml();
        employeeAIpml.add(e3);
        Employee[] employees = employeeAIpml.selectAll();
        for (int i=0;i<employeeAIpml.count;i++) {
            System.out.println(employees[i].getId()+"\t"+employees[i].getName()+"\t"+employees[i].getSal()+"\t"+employees[i].getDept());
        }
        employeeAIpml.update(e3);
        for (int i=0;i<employeeAIpml.count;i++) {
            System.out.println(employees[i].getId()+"\t"+employees[i].getName()+"\t"+employees[i].getSal()+"\t"+employees[i].getDept());
        }
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要查询的id");
        int id = sc.nextInt();
        Employee employee =employeeAIpml.selectById(id);
        System.out.println(employee.getId()+"\t"+employee.getName()+"\t"+employee.getSal()+"\t"+employee.getDept());
    }
}
